package com.lmk.mkcc.mq.consumer;

import com.lmk.mkcc.domain.CourseOrder;
import com.lmk.mkcc.enums.GlobalErrorCode;
import com.lmk.mkcc.service.ICourseOrderService;
import com.lmk.mkcc.util.AssertUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.Date;

@Slf4j
@Component
@RocketMQMessageListener(
        consumerGroup = "overtime-delayed-order-group",
        topic = "overtime-delayed-topic",
        selectorExpression = "overtime-delayed-tag",
        messageModel = MessageModel.BROADCASTING
)
public class OrderConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private ICourseOrderService courseOrderService;
    @Override
    public void onMessage(MessageExt messageExt) {
        // 1.业务校验
        byte[] body = messageExt.getBody();
        String orderNo = new String(body, StandardCharsets.UTF_8);
        AssertUtil.isNotEmpty(orderNo, GlobalErrorCode.DATA_NULL_ERROR);

        CourseOrder courseOrder = courseOrderService.getByorderNo(orderNo);
        if (courseOrder == null){
            return;
        }
        courseOrder.setUpdateTime(new Date());
        courseOrder.setStatusOrder(CourseOrder.CANCEL_PAY);
        courseOrderService.updateById(courseOrder);
    }
}
